﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using System.Collections;
using CodeReason.Reports;
using System.Data;
using System.IO;
using System.Windows.Xps.Packaging;
using OpenExpressApp.WPF.Controls;
using Ricciolo.Controls;
using System.Windows.Controls;

namespace OpenExpressApp.Module.WPF.Command.WpfReport
{
    public class WpfReportGen
    {
        /// <summary>
        /// 读取的模板
        /// </summary>
        private XElement _template;
        /// <summary>
        /// 保存路径
        /// </summary>
        private string _savePath;
        /// <summary>
        /// XName的根名称空间
        /// </summary>
        private string _rootNameSpace;
        /// <summary>
        /// XName的子名称空间
        /// </summary>
        private string _childNameSpace;
        /// <summary>
        /// 表头节点
        /// </summary>
        private XElement _tableHeader;
        /// <summary>
        /// 表数据节点
        /// </summary>
        private XElement _tableData;

        public WpfReportGen()
        {
            this._savePath = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"Templates/SimpleReport.xaml");
            this.ReadReportTemplate();
            this._rootNameSpace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
            this._childNameSpace = "clr-namespace:CodeReason.Reports.Document;assembly=CodeReason.Reports";
            this._tableHeader = _template.Descendants(RootName("TableRow")).ToArray()[0];
            this._tableData = _template.Descendants(ChildName("TableRowForDataTable")).ToArray()[0];
        }

        /// <summary>
        /// 根据根名称空间设置XName
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private XName RootName(string name)
        {
            return XName.Get(name, this._rootNameSpace);
        }
        /// <summary>
        /// 根据子名称空间设置XName
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private XName ChildName(string name)
        {
            return XName.Get(name, this._childNameSpace);
        }
        /// <summary>
        /// 根据路径读取模板
        /// </summary>
        /// <param name="path"></param>
        private void ReadReportTemplate()
        {
            _template = XElement.Load(_savePath);
        }
        /// <summary>
        /// 保存模板
        /// </summary>
        private void SaveTemplate()
        {
            _template.Save(_savePath);
        }
        /// <summary>
        /// 清空模板内容
        /// </summary>
        private void ClearTemplate()
        {
            if (this._tableHeader.Elements().Count() != 0)
                this._tableHeader.Elements().Remove();
            if (this._tableData.Elements().Count() != 0)
                this._tableData.Elements().Remove();
        }
        /// <summary>
        /// 根据视图生成模板
        /// </summary>
        public void GenerateTemplate(ObjectView view)
        {
            this.ClearTemplate();
            if (view.Control is DataGrid)
            {
                this.GenerateByDataGrid(view);
            }
            else if (view.Control is MultiObjectTreeView)
            {
                this.GenerateByTree(view);
            }
            this.SaveTemplate();
        }
        /// <summary>
        /// 根据MultiObjectTreeView生成模板
        /// </summary>
        /// <param name="view"></param>
        private void GenerateByTree(ObjectView view)
        {
            MultiObjectTreeView tree = view.Control as MultiObjectTreeView;
            TreeListView treeList = tree.Tree as TreeListView;
            foreach (var col in treeList.Columns)
            {
                //生成表头模板
                this.GenHeader(this._tableHeader, col.Header.ToString());
                //生成数据模板
                _tableData.SetAttributeValue("TableName", view.BOType.Name);
                this.GenData(this._tableData, col.Header.ToString());
            }
        }
        /// <summary>
        /// 根据DataGrid生成模板
        /// </summary>
        /// <param name="view"></param>
        private void GenerateByDataGrid(ObjectView view)
        {
            DataGrid grid = view.Control as DataGrid;
            foreach (var col in grid.Columns)
            {
                //生成表头模板
                this.GenHeader(this._tableHeader, col.Header.ToString());
                //生成数据模板
                _tableData.SetAttributeValue("TableName", view.BOType.Name);
                this.GenData(this._tableData, col.Header.ToString());
            }
        }
        /// <summary>
        /// 为节点添加表头模板
        /// </summary>
        /// <param name="element"></param>
        private void GenHeader(XElement element, string content)
        {
            XElement ele = new XElement(RootName("TableCell"), new XElement(RootName("Paragraph")));
            var node = ele.Descendants(RootName("Paragraph")).ToArray()[0];
            node.SetAttributeValue("TextAlignment", "Center");
            node.SetElementValue(RootName("Bold"), content);
            element.Add(ele);
        }
        /// <summary>
        /// 为节点添加数据模板
        /// </summary>
        /// <param name="element"></param>
        private void GenData(XElement element, string content)
        {
            XElement ele = new XElement(RootName("TableCell"), new XElement(RootName("Paragraph"), new XElement(ChildName("InlineTableCellValue"))));
            var node = ele.Descendants(ChildName("InlineTableCellValue")).ToArray()[0];
            node.SetAttributeValue("PropertyName", content);
            element.Add(ele);
        }
    }
}
